/**
 * 
 */
package com.faxsun.core.order.domain;

import java.util.Date;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.broadleafcommerce.common.money.Money;
import org.broadleafcommerce.common.presentation.AdminPresentation;
import org.broadleafcommerce.common.presentation.AdminPresentationClass;
import org.broadleafcommerce.common.presentation.PopulateToOneFieldsEnum;
import org.broadleafcommerce.common.presentation.client.SupportedFieldType;
import org.broadleafcommerce.common.presentation.override.AdminPresentationMergeEntry;
import org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverride;
import org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverrides;
import org.broadleafcommerce.common.presentation.override.PropertyType;
import org.broadleafcommerce.core.order.domain.OrderAttribute;
import org.broadleafcommerce.core.order.domain.OrderImpl;
import org.broadleafcommerce.core.order.service.type.OrderStatus;
import org.hibernate.annotations.Index;

import com.faxsun.core.order.service.type.FSInternalOrderStatus;
import com.faxsun.core.order.service.type.FSOrderStatus;

/**
 * 修改实体类中的OrderStatus类型
 * 
 * @author zhaoteng.song@faxsun.com
 *
 */
@Entity
@Table(name = "FS_BLC_ORDER")
@AdminPresentationMergeOverrides({
		@AdminPresentationMergeOverride(name = "", mergeEntries = @AdminPresentationMergeEntry(propertyType = PropertyType.AdminPresentation.READONLY, booleanOverrideValue = false, overrideValue = "false")),
		@AdminPresentationMergeOverride(name = "status", mergeEntries = @AdminPresentationMergeEntry(propertyType = PropertyType.AdminPresentation.BROADLEAFENUMERATION, overrideValue = "com.faxsun.core.order.service.type.FSOrderStatus"))

})
@AdminPresentationClass(populateToOneFields = PopulateToOneFieldsEnum.TRUE, friendlyName = "OrderImpl_FSOrder", ceilingDisplayEntity = "org.broadleafcommerce.core.order.domain.Order")
public class FSOrderImpl extends OrderImpl {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4608435878269886436L;

	/**
	 * 14天
	 */
	protected static final long cancellDuration = 14 * 24 * 3600 * 1000;

	@Column(name = "ORDER_STATUS")
	@Index(name = "FSORDER_STATUS_INDEX", columnNames = { "ORDER_STATUS" })
	@AdminPresentation(friendlyName = "OrderImpl_Order_Status", group = Presentation.Group.Name.General, order = Presentation.FieldOrder.STATUS, prominent = true, fieldType = SupportedFieldType.BROADLEAF_ENUMERATION, broadleafEnumeration = "com.faxsun.core.order.service.type.FSOrderStatus", groupOrder = Presentation.Group.Order.General, gridOrder = 3000)
	protected String status;

	@Column(name = "FSINTERNAL_ORDER_STATUS")
	@Index(name = "FSINTERNAL_ORDER_STATUS_INDEX", columnNames = { "FSINTERNAL_ORDER_STATUS" })
	@AdminPresentation(friendlyName = "OrderImpl_Order_InternalStatus", group = Presentation.Group.Name.General, order = Presentation.FieldOrder.STATUS, prominent = true, fieldType = SupportedFieldType.BROADLEAF_ENUMERATION, broadleafEnumeration = "com.faxsun.core.order.service.type.FSInternalOrderStatus", groupOrder = Presentation.Group.Order.General, gridOrder = 3000)
	protected String internalStatus;

	@Column(name = "ORDER_SIGNED", nullable = true)
	@AdminPresentation(friendlyName = "OrderImpl_Order_Signed_Date", group = Presentation.Group.Name.General, order = Presentation.FieldOrder.SUBMITDATE, groupOrder = Presentation.Group.Order.General, prominent = true, gridOrder = 5000)
	protected Date signedData;

	/**
	 * 订单可退货，true为可退货；false为不可退货
	 */
	@Transient
	protected boolean isCancellable = false;

	@Override
	public FSOrderStatus getStatus() {
		return FSOrderStatus.getInstance(status);
	}

	@Override
	public void setStatus(OrderStatus status) {
		this.status = status.getType();
	}

	public FSInternalOrderStatus getInternalStatus() {
		return FSInternalOrderStatus.getInstance(internalStatus);
	}

	public void setInternalStatus(FSInternalOrderStatus internalStatus) {
		this.internalStatus = internalStatus.getType();
	}

	/**
	 * 订单可退货<br/>
	 * 当前时间距签收时间在14*24小时时间内
	 * 
	 * @return true为可退货；false为不可退货
	 */
	public boolean isCancellable() {

		if (signedData == null) {
			return isCancellable;
		}

		long duration = System.currentTimeMillis() - signedData.getTime();

		if (duration < cancellDuration && duration > 0) {
			isCancellable = true;
		}
		return isCancellable;
	}

	@Override
	public Money getTotalShipping() {
		Money money = getTotalFulfillmentCharges();
		return money == null ? Money.ZERO : money;
	}

	@Override
	public String toString() {
		StringBuffer orderString = new StringBuffer("FSOrderImpl [ id=" + id
				+ ", customerId=" + customer.getId() + ", email="
				+ this.emailAddress + ", orderNumber=" + this.getOrderNumber()
				+", createdTime="+this.auditable.getDateCreated()
				+", updaedTime="+this.auditable.getDateUpdated()
				+ ", items=" + this.orderItems + ", status=" + status
				+ ", internalStatus=" + internalStatus
				);

		// orderAttributes;
		Map<String, OrderAttribute> attributes = this.getOrderAttributes();
		orderString.append(", orderAttributes=[");
		for (OrderAttribute oa : attributes.values()) {
			orderString.append("[oaId=" + oa.getId() + ", name=" + oa.getName()
					+ ", value=" + oa.getValue() + "]");
		}
		orderString.append("]");

		orderString.append("]");
		return orderString.toString();

	}

}
